package com.ctg.itrdc.imageconsole.common.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 * @Description 日期时间工具类
 * @author <a href="mailto:18070514058@163.com">Piao.Wu</a>
 * @Data 2016年4月13日
 * @Version 1.0.0
 */
public class TimeUtils {

    private static SimpleDateFormat default_format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    //private static SimpleDateFormat format_YMD=new SimpleDateFormat("yyyy-MM-dd");
    
    private static Calendar calendar = null;
    
    
    public static String getNowDateStr(){
    	return default_format.format(new Date());
    }
    
    public static String format(String format,Date date){
        SimpleDateFormat formater=new SimpleDateFormat(format);
        return formater.format(date);
    }
    
    public static Date parse(String format,String date){
        SimpleDateFormat formater=new SimpleDateFormat(format);
        try {
            return formater.parse(date);
        } catch (ParseException e) {}
        return null;
    }
    
    public static Date parse(String date){
        try {
            return default_format.parse(date);
        } catch (ParseException e) {}
        return null;
    }
    
    public static Date systemTime(){
        return new Date();
    }
	
	public static Timestamp getSystemTimestamp(){
		Timestamp d = new Timestamp(System.currentTimeMillis());
		return d;
	}
	
	public static String fomatTimeMillisToDateStr(Long timeMillis){
		return default_format.format(new Date(timeMillis));
	}
	
	public static String getYear(Date date) {
	    return default_format.format(date).substring(0, 4);
	}

	
	public static int getMonth(Date date) {
	    calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    return calendar.get(Calendar.MONTH) + 1;
	}
	 
	public static String getWeekOfDate(Date date) {
        String[] weekDays = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }
	
	public static int getDay(Date date) {
	    calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    return calendar.get(Calendar.DAY_OF_MONTH);
	}
	 
	public static int getHour(Date date) {
	    calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    return calendar.get(Calendar.HOUR_OF_DAY);
	}
	 
	public static int getMinute(Date date) {
	    calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    return calendar.get(Calendar.MINUTE);
	}
	 
	public static int getSecond(Date date) {
	    calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    return calendar.get(Calendar.SECOND);
	}
	 
}

